<?php

class Ext_Publish_Attachment_Order
{
    protected $userId;
    protected $articleId;
    
    protected $messager;
    const ARTICLE_RECORD_NOT_FOUND   = 'articleRecordNotFound';
    const INVALID                    = 'invalid';
    protected $_messageTemplates = array(
        self::ARTICLE_RECORD_NOT_FOUND   => "Article record not found.",
        self::INVALID                    => "Invalid.",
    );
    
    public function __construct($articleId)
    {
        $this->_articleId  = $articleId;
        
        $this->_messager = new Ext_Messager($this->_messageTemplates);
    }
    
    public function order($newOrder)
    {
        $result = array(
            'code'      => Ext_Publish_Attachment_Order_Result::FAILURE,
            'messages'  => array(),
        );
        
        $articleRow = App_Model_DbTable_Article::getInstance()->getRowById($this->_articleId);
        if(!$articleRow){
            $result['messages'][] = $this->_messager->addMessage(self::ARTICLE_RECORD_NOT_FOUND);
            return new Ext_Publish_Attachment_Order_Result($result['code'], $result['messages']);
        }
        
        $attachments = $articleRow['attachments'];
        $oldAttachmentsArray = unserialize($attachments);
        $newAttachmentsArray = $newAttachment = $oldAttachment = array();
        for($i=0;$i<count($oldAttachmentsArray);$i++){
            $attachment = $oldAttachmentsArray[$i];
            $attachmentType   = $attachment['type'];
            $attachmentDataId = $attachment['id'];
            
            if(!$attachmentType){
                continue;
            }
            if(!$attachmentDataId){
                continue;
            }
            if(!isset($oldAttachment[$attachmentType])){
                $oldAttachment[$attachmentType] = array();
            }
            if(in_array($attachmentDataId, $oldAttachment[$attachmentType])){
                continue;
            }
            $oldAttachment[$attachmentType][] = $attachmentDataId;
        }
        
        ksort($newOrder);
        $newAttachmentsArray = $newAttachment = array();
        for($i=0;$i<count($newOrder);$i++)
        {
            $attachment = $newOrder[$i];
            $attachmentType   = $attachment['type'];
            $attachmentDataId = $attachment['id'];

            if(!$attachmentType){
                continue;
            }
            if(!$attachmentDataId){
                continue;
            }
            if(!isset($newAttachment[$attachmentType])){
                $newAttachment[$attachmentType] = array();
            }
            if(in_array($attachmentDataId, $newAttachment[$attachmentType])){
                continue;
            }
            if(!isset($oldAttachment[$attachmentType])){
                $oldAttachment[$attachmentType] = array();
            }
            if(!in_array($attachmentDataId, $oldAttachment[$attachmentType])){
                $result['messages'][] = $this->_messager->addMessage(self::INVALID);
                return new Ext_Publish_Attachment_Order_Result($result['code'], $result['messages']);
            }

            $newAttachment[$attachmentType][] = $attachmentDataId;
	    $newAttachmentsArray[$i] = array("type"=>$attachmentType, "id"=>$attachmentDataId);
	}
        //print_r($newAttachment);
        for($i=0;$i<count($oldAttachmentsArray);$i++){
            $attachment = $oldAttachmentsArray[$i];
            $attachmentType   = $attachment['type'];
            $attachmentDataId = $attachment['id'];
            
            if(!$attachmentType){
                continue;
            }
            if(!$attachmentDataId){
                continue;
            }

            if(!isset($newAttachment[$attachmentType])){
                $newAttachment[$attachmentType] = array();
            }
            if(!in_array($attachmentDataId, $newAttachment[$attachmentType])){
                $result['messages'][] = $this->_messager->addMessage(self::INVALID);
                return new Ext_Publish_Attachment_Order_Result($result['code'], $result['messages']);
            }
        }
        //print_r($newAttachment);
        if($oldAttachmentsArray){
            $row = array(
                    'attachments' => serialize($newAttachmentsArray),
            );
            App_Model_DbTable_Article::getInstance()->addOld($row, $this->_articleId);
        }
        
        $result['code']  = Ext_Publish_Attachment_Order_Result::SUCCESS;
        return new Ext_Publish_Attachment_Order_Result($result['code'], $result['messages']);
    }
}
